package com.autumn.platform.dao.schema;

import org.apache.ibatis.executor.parameter.ParameterHandler;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.scripting.xmltags.XMLLanguageDriver;
import org.apache.ibatis.scripting.xmltags.XMLScriptBuilder;
import org.apache.ibatis.session.Configuration;

import com.autumn.platform.dao.component.impl.MybatisComponents;

public class SchemaXMLLanguageDriver extends XMLLanguageDriver {

    /**
     * 调用工厂类创建参数处理器
     */
    @Override
    public ParameterHandler createParameterHandler(MappedStatement mappedStatement, Object parameterObject,
            BoundSql boundSql) {
        return MybatisComponents.newParameterHandler(mappedStatement, parameterObject, boundSql);
    }

    /**
     * 调用工厂类创建脚本级元素解析器，并解析为SqlSource返回
     */
    @Override
    public SqlSource createSqlSource(Configuration configuration, XNode script, Class<?> parameterType) {
        /**
         * 如果script是SchemaXNode，则返回SchemaXMLScriptBuilder，否则返回XMLScriptBuilder
         */
        XMLScriptBuilder builder = MybatisComponents.newXMLScriptBuilder(configuration, script, parameterType);
        return builder.parseScriptNode();
    }
}
